/**
 * 
 */   
package com.cares.infobird.service;  

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.cares.core.base.service.BaseService;
import com.cares.core.utils.StringUtil;
import com.cares.entity.base.PageModel;
import com.cares.entity.infobird.CustomerInfo;
import com.cares.entity.infobird.Location;
import com.cares.infobird.repository.CustomerInfoMybatisDao;

/**   
 * @作者 Stone
 * @创建日期 2014-7-26 下午12:04:33
 * @版本 V1.0
 * @类说明
 */
@Service("customerInfoService")
@Transactional(readOnly = true)
public class CustomerInfoService extends BaseService {

	@Autowired
	private CustomerInfoMybatisDao customerInfoMybatisDao;
	
	@Resource(name="locationService")
	private LocationService locationService;
	
	/**
	 * 查询列表信息
	 * @param pageModel
	 * @return
	 */
	public String list(PageModel<CustomerInfo> pageModel) {

		String result = "";
		JSONObject json = new JSONObject();
		List<CustomerInfo> results = customerInfoMybatisDao.getAll(pageModel);
		try {
			json.put("total", this.customerInfoMybatisDao.count(pageModel));
			json.put("rows", JSON.toJSON(results));
			result = json.toString();
		} catch (JSONException e) {
		}
		return result;
	}
	
	/**
	 * 根据ID查询CustomerInfo
	 */
	public CustomerInfo get(Long id) {
		return customerInfoMybatisDao.get(id);
	}
	
	/**
	 * 根据mobile查询CustomerInfo
	 */
	public CustomerInfo getByMobile(String mobile) {
		return customerInfoMybatisDao.getByMobile(mobile);
	}
	
	/**
	 * 根据phoneNo查询CustomerInfo
	 */
	public CustomerInfo getByPhoneNo(String phoneNo) {
		return customerInfoMybatisDao.getByPhoneNo(phoneNo);
	}
	
	/**
	 * 根据mobile查询CustomerInfo
	 */
	public CustomerInfo getByAcctNo(String acctNo) {
		return customerInfoMybatisDao.getByAcctNo(acctNo);
	}
	
	/**
	 * 新增CustomerInfo
	 */
	public void save(CustomerInfo info) {
		if(StringUtil.isNotEmpty(info.getMobile()) && StringUtil.isEmpty(info.getProvince())) {
			Location location = locationService.find(info.getMobile());
			info.setProvince(location.getProvince());
			info.setCity(location.getCity());
			if(StringUtil.isNotEmpty(info.getProvince())) {
				info.setDependency(location.getProvince() + "-" + location.getCity());
			}
		}
		info.setCreateTime(dateProvider.getDate());
		customerInfoMybatisDao.save(info);
	}
	
	/**
	 * 更新CustomerInfo
	 */
	public void update(CustomerInfo info) {
		Assert.notNull(info.getId(), "缺少主键");
		info.setUpdateTime(dateProvider.getDate());
		customerInfoMybatisDao.update(info);
	}
	
	/**
	 * 根据ID删除CustomerInfo
	 */
	public void delete(Long id) {
		customerInfoMybatisDao.delete(id);
	}
}
